//
//  KWTPolicyListVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/15.
//

#import "KWTPolicyListVC.h"
#import "RHSearchView.h"
#import "KWTPolicyListCell.h"
#import "KWTPolicyDetailVC.h"
#import "KWTVppSetRecordModel.h"

@interface KWTPolicyListVC ()<UITableViewDelegate,UITableViewDataSource,QMUITextFieldDelegate>
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *recordArray;

@property (nonatomic, strong) RHSearchView *searchView;

@property (nonatomic, assign) RequestType requestType;
//当前页数
@property (nonatomic, assign) NSInteger currentPage;
//总页数
@property (nonatomic, assign) NSInteger totalPage;
//每页多少条数据
@property (nonatomic, assign) NSInteger numOfPage;

@property (nonatomic ,copy) NSString *keyCode;
@end

@implementation KWTPolicyListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"运行策略".rh_localized;
    self.requestType = RequestTypeRefresh;
    self.currentPage = 1;
    self.numOfPage = 20;
    self.recordArray = [NSMutableArray array];
    [self setupUI];
    [self getVppSetRecord];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)setupUI{
    kWeakSelf;
    //搜索栏
    _searchView = [[RHSearchView alloc]initWithFrame:CGRectMake(5, 0, kUI_WINDOW_W-10, 55)];
    [self.view addSubview:_searchView];
    _searchView.delegate = self;
    _searchView.searchTextField.placeholder = @"请输入关键字".rh_localized;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView registerClass:[KWTPolicyListCell class] forCellReuseIdentifier:@"KWTPolicyListCell"];
    [self.view addSubview:_tableView];
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.searchView.mas_bottom);
        make.bottom.mas_offset(0);
    }];
    

    [_tableView addHeaderRefresh:^{
        [weakSelf.tableView.mj_footer resetNoMoreData];
        weakSelf.requestType = RequestTypeRefresh;
        weakSelf.currentPage = 1;
        weakSelf.keyCode = @"";
        [weakSelf getVppSetRecord];
        
    }];
    
    [_tableView addBackFooterRefresh:^{
        weakSelf.requestType = RequestTypeAdd;
        weakSelf.currentPage++;
        [weakSelf getVppSetRecord];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.recordArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWTPolicyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KWTPolicyListCell"];
    cell.recordModel = [self.recordArray safeObjectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KWTPolicyDetailVC *detailVC = [[KWTPolicyDetailVC alloc]init];
    detailVC.recordModel = [self.recordArray safeObjectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark ---------- 获取分组设置记录列表 -----------
-(void)getVppSetRecord
{
  
    [QMUITips showLoadingInView:self.view];
    
    
    NSDictionary *para = @{@"pageNum":@(self.currentPage),
                           @"pageSize":@(self.numOfPage),
                           @"groupName":ValidNullData(self.keyCode)
    };
    
    
    [KWTVppSetRecordModel startGetWithCompletionAPIKey:H_vppSetRecords argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            NSInteger total = [request.responseObject[@"total"] integerValue];
            self.totalPage = total%self.numOfPage? total/self.numOfPage+1 : total/self.numOfPage;
            NSMutableArray *arr = [NSMutableArray arrayWithArray:resultModel.data];
            //下拉刷新
            if(self.requestType == RequestTypeRefresh){
                [self.tableView endHeaderRefresh];
                self.recordArray = arr;
            }else{//上拉加载
                [self.tableView endFooterRefresh];
                [self.recordArray addObjectsFromArray:arr];
            }
            if(self.currentPage >= self.totalPage){
                [self.tableView endFooterRefreshWithNoMoreData];
            }
            [self.tableView reloadData];
            if(self.recordArray.count == 0){
                [self.tableView endFooterRefreshWithNoMoreData];
            }
        }else{
            [self.tableView endFooterRefresh];
        }

    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [QMUITips hideAllToastInView:self.view animated:YES];
        [self.tableView endHeaderRefresh];
    }];
    
    
    
}

#pragma mark ------- textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.keyCode = textField.text;
    [self getVppSetRecord];
    [self.searchView resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    self.keyCode = @"";
    [self getVppSetRecord];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.searchView resignFirstResponder];
    });
    return YES;
}
@end
